#include "mera.h"
#include "qdebug.h"

//append
quint64 Parametr::append(float x, float y)  //слишком долго: 70000 appendов занимают 5 секунд
{
    size_t x_size=sizeof(float);
    size_t y_size=sizeof(float);
    size_t rez=-1;

    if(!m_yData)
         m_yData = new QByteArray;

    if(!m_xData)
         m_xData = new QByteArray;
    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}
quint64 Parametr::append(double x, double y)
{
    size_t x_size=sizeof(double);
    size_t y_size=sizeof(double);
    size_t rez=-1;
     if(!m_yData)
          m_yData = new QByteArray;

     if(!m_xData)
          m_xData = new QByteArray;

    QFile dat_file(this->path() + ".dat");

    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}


quint64 Parametr::append(double y)
{

    size_t y_size=sizeof(double);
    size_t rez=-1;

    if(!m_yData)
        m_yData = new QByteArray;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }


    return y_size;
}


quint64 Parametr::append(char x, char y)
{
    size_t x_size=sizeof(char);
    size_t y_size=sizeof(char);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}

quint64 Parametr::append(short x, short y)
{
    size_t x_size=sizeof(short);
    size_t y_size=sizeof(short);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}
quint64 Parametr::append(int x, int y)
{
    size_t x_size=sizeof(int);
    size_t y_size=sizeof(int);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}
quint64 Parametr::append(long  x, long  y)
{
    size_t x_size=sizeof(long);
    size_t y_size=sizeof(long);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}
quint64 Parametr::append(long long  x, long long  y)
{
    size_t x_size=sizeof(long long);
    size_t y_size=sizeof(long long);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}

quint64 Parametr::append(unsigned char x, unsigned char y)
{
    size_t x_size=sizeof(unsigned char);
    size_t y_size=sizeof(unsigned char);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}
quint64 Parametr::append(unsigned short x,unsigned short y)
{
    size_t x_size=sizeof(unsigned short);
    size_t y_size=sizeof(unsigned short);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}
quint64 Parametr::append(unsigned int x, unsigned  int y)
{
    size_t x_size=sizeof(unsigned int);
    size_t y_size=sizeof(unsigned int);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}
quint64 Parametr::append(unsigned long x, unsigned  long y)
{
    size_t x_size=sizeof(unsigned long);
    size_t y_size=sizeof(unsigned long);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}
quint64 Parametr::append(unsigned long long x, unsigned  long long y)
{
    size_t x_size=sizeof(unsigned long long);
    size_t y_size=sizeof(unsigned long long);
    size_t rez=-1;

    QFile dat_file(this->path() + ".dat");
    if(!dat_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +dat_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=dat_file.write((char*)&y,y_size);
    dat_file.close();

    if(rez == y_size){
        m_yData->append((char*)&y,y_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +dat_file.errorString());
        qCritical() << this->errorString();
    }

    QFile x_file(this->path() + ".x");
    if(!x_file.open(QIODevice::Append)){
        this->setErrorString(this->path() + " " +x_file.errorString());
        qCritical() << this->errorString();
        return -1;
    }

    rez=x_file.write((char*)&x,x_size);
    x_file.close();

    if(rez == x_size){
        m_xData->append((char*)&x,x_size);
    }else{
        this->setErrorString(this->path() + tr(" ") +x_file.errorString());
        qCritical() << this->errorString();
    }


    return x_size + y_size;
}


Parametr &Parametr::operator<<(const char &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(char)-1;
    char x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const float &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(float)-1;
    float x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const double &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(double)-1;
    double x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const short &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(short)-1;
    short x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const int &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(int)-1;
    int x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const long &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(long)-1;
    long x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const long long &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(long long)-1;
    long long x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const unsigned short &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(unsigned short)-1;
    unsigned short x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const unsigned int &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(unsigned int)-1;
    unsigned int x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const unsigned long &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(unsigned long)-1;
    unsigned long x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
Parametr &Parametr::operator<<(const unsigned long long &y)
{
    size_t x_size=m_xData->size();
    size_t x_index=x_size-sizeof(unsigned long long)-1;
    unsigned long long x = m_xData->data()[x_index];

    this->append(x,y);
    return *this;
}
